package com.blueray.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author ivanhuang
 * @create 2020/12/24 11:29
 **/
@Configuration
public class GulimallCrosConfiguration {

    /**
     * Cors跨越访问统一处理
     * 1.跨域头部信息
     * 2.跨域方法
     * 3.跨域源
     * 4.携带cookie
     * @return
     */
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfig = new CorsConfiguration();
        corsConfig.addAllowedHeader("*");
        corsConfig.addAllowedMethod("*");
        corsConfig.addAllowedOrigin("*");
        //允许携带cookie
        corsConfig.setAllowCredentials(true);
        configSource.registerCorsConfiguration("/**",corsConfig);
        return new CorsWebFilter(configSource);
    }
}
